﻿using EYangFrame.DataAccess.Global;
using EYangFramewirk.Business.MessageSystem.Interfaces;
using EYangFramework.Common.Ninject;
using EYangFramework.Domain.Entitys.Entrys;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace EYangFramework.MessageSystem
{
    public partial class frm_EditMessageModel : EYangFramework.CommonForm.frm_BasicForm
    {
        bool isAdd = false;
        public frm_EditMessageModel()
        {
            this.AllowSingtonTimer = false;
            InitializeComponent();
        }

        private void frm_EditMessageModel_Load(object sender, EventArgs e)
        {
            SetTabVisible(tp_Detail, false);
            AllowSingtonTimer = false;
            BindList();

        }
        public override MenuStrip GetMenuMain()
        {
            return this.menuStrip1;
        }
        private void BindList()
        {
            using (IBLLEditMessageModel bLLEditMessageModel = NinjectProvider.NinjectCore.GetService<IBLLEditMessageModel>())
            {
                List<MessageModel> messageModels = bLLEditMessageModel.GetAllMessageModel(GlobalInfo.CurrentUser.USERNAME);
                GridList.DataSource = messageModels;
            }
        }

        private void BindMessageModel(MessageModel model)
        {
            txtModelName.Text = model?.ModelName;
            txtMessageContent.Text = model?.MessageModelContent;
        }

        private void menu_View_Click(object sender, EventArgs e)
        {
            if (GridList.SelectedRows.Count <= 0)
            {
                MessageBox.Show("请选择需要查看的模板!", "提示");
                return;
            }
            SetTabVisible(tp_Detail, true);
            panel1.Enabled = false;
        }

        private void menu_Add_Click(object sender, EventArgs e)
        {
            SetTabVisible(tp_Detail, true);
            panel1.Enabled = true;
            txtModelName.ReadOnly = false;
            BindMessageModel(null);
            panel1.Tag = null;
            isAdd = true;
        }

        private void menu_Edit_Click(object sender, EventArgs e)
        {
            if (GridList.SelectedRows.Count <= 0)
            {
                MessageBox.Show("请选择需要修改的模板!", "提示");
                return;
            }
            SetTabVisible(tp_Detail, true);
            panel1.Enabled = true;
            txtModelName.ReadOnly = true;
            isAdd = false;
            using (IBLLEditMessageModel bLLEditMessageModel = NinjectProvider.NinjectCore.GetService<IBLLEditMessageModel>())
            {
                string id = GridList.SelectedRows[0].Cells["ID"].Value.ToString();
                MessageModel model = bLLEditMessageModel.GetMessage(Guid.Parse(id));
                panel1.Tag = id;
                BindMessageModel(model);
            }
        }

        private void menu_Delete_Click(object sender, EventArgs e)
        {
            if (GridList.SelectedRows.Count <= 0)
            {
                MessageBox.Show("请选择需要删除的模板!", "提示");
                return;
            }
            if (MessageBox.Show("您确定要删除选中的模板吗?", "警告", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                using (IBLLEditMessageModel bLLEditMessageModel = NinjectProvider.NinjectCore.GetService<IBLLEditMessageModel>())
                {
                    bLLEditMessageModel.DeleteMessageModel(Guid.Parse(GridList.SelectedRows[0].Cells["ID"].Value.ToString()));
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtMessageContent.Text) || string.IsNullOrEmpty(txtModelName.Text))
            {
                MessageBox.Show("模板名称或者模板内容不能为空!", "警告");
                return;
            }
            MessageModel model;
            bool isSuccess = false;
            if (isAdd)
            {
                model = new MessageModel();
                model.User = GlobalInfo.CurrentUser.USERNAME;
                model.MessageModelContent = txtMessageContent.Text;
                model.ModelName = txtModelName.Text;
                model.CREATEUSER = GlobalInfo.CurrentUser.USERNAME;
                using (IBLLEditMessageModel bLLEditMessageModel = NinjectProvider.NinjectCore.GetService<IBLLEditMessageModel>())
                {
                    isSuccess = bLLEditMessageModel.InsrtMessageModel(model);
                }
            }
            else
            {
                using (IBLLEditMessageModel bLLEditMessageModel = NinjectProvider.NinjectCore.GetService<IBLLEditMessageModel>())
                {
                    model = bLLEditMessageModel.GetMessage(Guid.Parse(panel1.Tag.ToString()));
                    model.MessageModelContent = txtMessageContent.Text;
                    isSuccess = bLLEditMessageModel.UpdateMessageModel(model);
                }
            }
            if (isSuccess)
            {
                MessageBox.Show("保存成功!", "提示");
                btnCancel_Click(null, null);
            }
            else
            {
                MessageBox.Show("保存失败!", "提示");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (sender == null || MessageBox.Show("你确定要退出编辑模式吗?", "提醒!", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                SetTabVisible(tp_Detail, false);
                BindList();
            }
        }
    }
}
